package com.aaa.controller;

import com.aaa.config.NonStaticResourceHttpRequestHandler;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @ClassName: FileRestController
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/9/10 16:34
 * @Version:1.0
 **/
@RestController
@RequestMapping("file")
@AllArgsConstructor
public class FileRestController {
    private final NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;


    @GetMapping("video")
    public void videoPreview(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        String sourcePath = ClassUtils.getDefaultClassLoader().getResource("").getPath().substring(1);
        String realPath = "E:\\录屏作业\\1658200022.mp4";
        Path filePath = Paths.get(realPath);
        if (Files.exists(filePath)) {
            String mimeType = Files.probeContentType(filePath);
            if (!ObjectUtils.isEmpty(mimeType)) {
                response.setContentType(mimeType);
            }
            System.out.println("----------请求成功");
            request.setAttribute(NonStaticResourceHttpRequestHandler.ATTR_FILE, filePath);
            nonStaticResourceHttpRequestHandler.handleRequest(request, response);
        } else {
            System.out.println("--------请求失败");
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        }
    }
}
